#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>
#include <linux/fb.h>

#include <linux/input.h>//触摸屏的头文件

#include "lcd.h"

//液晶屏设备     触摸屏设备 


int main()
{
    lcd_init();

    lcd_draw_rect(0,0,800,480,WHITE);

    int x,y;
     //1 打开触摸屏设备文件  touch sreen
    int tsFd = open("/dev/input/event0", O_RDWR);
    if(tsFd == -1)
    {
        perror("open touch error");
        return -1;
    }
    //2 读取触摸屏设备的坐标
    while(1)
    {
        struct input_event info;
        read(tsFd,&info,sizeof(info));
        
        //判断是触摸屏事件 而且是X轴事件
        if(info.type == EV_ABS && info.code == ABS_X)
        {
            x = info.value;
        }
        //判断是触摸屏事件 而且是Y轴事件
        if(info.type == EV_ABS && info.code == ABS_Y)
        {
            y = info.value;
        }
        //3 将触摸屏的坐标打印出来
        //printf("type:%d, code:%d, value:%d\n", info.type, info.code, info.value);
        //坐标轨迹追踪
        if(info.type == 0 && info.code == 0 && info.value == 0)
        {  
            x =  ((x) * 800 *1.0) / 1024; 
            y =  ((y) * 480 *1.0) / 600;
            printf("x = %d, y = %d\n", x, y);   
      
            //画一个矩形
            lcd_draw_rect(x, y,10,10, RED);
        }

    }


    //4 关闭触摸屏文件
    close(tsFd);
    


}